package com.ys.service.business.impl;

import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.alibaba.fastjson.JSON;
import com.ys.entity.business.TpShopRegisterPhaseSet;
import com.ys.mapper.business.TpShopRegisterPhaseSetMapper;
import com.ys.service.business.TpShopRegisterPhaseSetService;

@Service("tpShopRegisterPhaseSetService")
public class TpShopRegisterPhaseSetServiceImpl implements TpShopRegisterPhaseSetService {


	@Autowired
	private TpShopRegisterPhaseSetMapper tpShopRegisterPhaseSetMapper;

	/**
	 * 新增
	 * @param params
	 * @return
	 */
	@Override
	public int add(Map<String, Object> params) {
		TpShopRegisterPhaseSet entity = JSON.parseObject(JSON.toJSONString(params), TpShopRegisterPhaseSet.class);
		entity.setStatus(1);
		String parentNodeInfo = params.get("parentNodeInfo")+"";
		if(parentNodeInfo!=null && !parentNodeInfo.equals("")){
			entity.setParentNode(parentNodeInfo);
		}
		int phaseNum = Integer.parseInt(params.get("phaseNum").toString());
		int parentId = Integer.parseInt(params.get("parentId").toString());
		//查询是否有需要修改阶段字段的信息，排序信息
		List<TpShopRegisterPhaseSet> hasNeedUpdateList = new TpShopRegisterPhaseSet()
				.where("A.PhaseNum >= "+ phaseNum + " AND A.ParentId = " + parentId)
				.queryList();
		//如果有需要修改
		if( hasNeedUpdateList != null && hasNeedUpdateList.size()>0 ){
			for( int i=0;i<hasNeedUpdateList.size();i++ ){
				TpShopRegisterPhaseSet changeMsg = new TpShopRegisterPhaseSet();
				changeMsg.setId(hasNeedUpdateList.get(i).getId());
				changeMsg.setPhaseNum(phaseNum + i + 1);
				//修改
				tpShopRegisterPhaseSetMapper.update(changeMsg);
			}
		}
		return tpShopRegisterPhaseSetMapper.insert(entity);
	}

	/**
	 * 修改
	 * @param params
	 * @return
	 */
	@Override
	public int edit(Map<String, Object> params) {

		//新的阶段号
		int phaseNum = Integer.parseInt(params.get("phaseNum").toString());
		//旧的阶段号
		int oldPhaseNum = Integer.parseInt(params.get("oldPhaseNum").toString());
		//父id
		int parentId = Integer.parseInt(params.get("parentId").toString());
		//id
		Long id = Long.parseLong(params.get("id").toString());

		String oldSetType =  params.get("oldSetType").toString();
		String setType =  params.get("setType").toString();
		String childrenLength =  params.get("childrenLength").toString();
		//一些类型新旧发生变化的时候，需要删除子数据
		//分支集合（有子集的时候）-》任务分支、阶段任务
		//任务分支（有子集的时候）-》分支集合、阶段任务
		/*
		if(((oldSetType.equals("1") && !setType.equals("1"))||(oldSetType.equals("2") && !setType.equals("2"))) && !childrenLength.equals("0")){
			List<TpShopRegisterPhaseSet> hasNeedDelList = new TpShopRegisterPhaseSet().queryList();
			//递归查询,然后删除
			hasNeedDelList.stream().filter(item -> item.getId().equals(id) ).map(item -> delChildrens(item,hasNeedDelList));

		}
		 */
		//如果阶段号修改了，则需要修改其他有被影响的阶段号
		if( phaseNum!=oldPhaseNum ){
			int smallNum = ( phaseNum>oldPhaseNum ? oldPhaseNum : phaseNum );
			int bigNum = ( phaseNum>oldPhaseNum ? phaseNum : oldPhaseNum );

			//查询是否有需要修改阶段顺序的字段的信息
			List<TpShopRegisterPhaseSet> hasNeedUpdateList = new TpShopRegisterPhaseSet()
					.where("A.PhaseNum BETWEEN "+ smallNum + " AND " + bigNum)
					.where("A.ParentId = " + parentId)
					.queryList();

			//如果有需要修改
			if( hasNeedUpdateList != null && hasNeedUpdateList.size()>0 ){
				int i = 0;
				int maxNum = hasNeedUpdateList.size();
				int countNum = 0;
				if(phaseNum>oldPhaseNum){//新>旧，则在新与旧之间的阶段都-1
					i = 1;
					countNum = -1;
					maxNum = hasNeedUpdateList.size();
				}else {//新<旧，则在新与旧之间的阶段都+1
					//i = 0;
					countNum = 1;
					maxNum = hasNeedUpdateList.size();
				}
				for( ;i<maxNum;i++ ){
					TpShopRegisterPhaseSet changeMsg = new TpShopRegisterPhaseSet();
					changeMsg.setId(hasNeedUpdateList.get(i).getId());
					changeMsg.setPhaseNum(hasNeedUpdateList.get(i).getPhaseNum()+countNum);
					changeMsg.setStatus(1);
					//修改
					tpShopRegisterPhaseSetMapper.update(changeMsg);
				}
			}
		}
		//修改
		TpShopRegisterPhaseSet entity = JSON.parseObject(JSON.toJSONString(params), TpShopRegisterPhaseSet.class);
		entity.setStatus(1);
		String parentNodeInfo = params.get("parentNodeInfo")+"";
		if(parentNodeInfo!=null && !parentNodeInfo.equals("")){
			entity.setParentNode(parentNodeInfo);
		}
		return tpShopRegisterPhaseSetMapper.update(entity);
	}

	/**
	 * 查询子类数据,删除
	 * @param item
	 * @param dateList
	 * @return
	 */
	public List<TpShopRegisterPhaseSet> delChildrens(TpShopRegisterPhaseSet item,List<TpShopRegisterPhaseSet> dateList){
		List<TpShopRegisterPhaseSet> childrens = dateList.stream()
				.filter(dataItem -> dataItem.getParentId().equals(item.getId()))
				.map(childrenItem -> {
					tpShopRegisterPhaseSetMapper.deleteTrue(childrenItem.getId());//删除
					delChildrens(childrenItem,dateList);
					return childrenItem;
				})
				.collect(Collectors.toList());
		return childrens;
	}

	/**
	 * 删除
	 * @param id
	 * @param phaseNum
	 * @return
	 */
	@Override
	public void deleteTrue(long id,int phaseNum,long parentId) {
		//查询是否有需要修改阶段字段的信息
		List<TpShopRegisterPhaseSet> hasNeedUpdateList = new TpShopRegisterPhaseSet()
				.where("A.PhaseNum >"+ phaseNum)
				.where("A.ParentId = " + parentId)
				.queryList();
		//如果有需要修改
		if( hasNeedUpdateList != null && hasNeedUpdateList.size()>0 ){
			for( int i=0;i<hasNeedUpdateList.size();i++ ){
				TpShopRegisterPhaseSet changeMsg = new TpShopRegisterPhaseSet();
				changeMsg.setId(hasNeedUpdateList.get(i).getId());
				changeMsg.setPhaseNum(phaseNum + i);
				//修改
				tpShopRegisterPhaseSetMapper.update(changeMsg);
			}
		}
		//删除
		tpShopRegisterPhaseSetMapper.deleteTrue(id);
	}
}
